//
//  DCAuthPhotoController.m
//  Demo
//
//  Created by zhaolin on 2023/1/9.
//

#import "DCAuthPhotoController.h"
#import "DCTakePhotoController.h"
#import "DCAuthFooterView.h"
#import "DCAuthTipsCell.h"

@interface DCAuthPhotoController ()<UITableViewDelegate,UITableViewDataSource,TakePhotoDelegate>

@property (nonatomic, strong) UIImageView *statusImage;

@property (nonatomic, strong) UIImageView *userPhotoImage;
@end

@implementation DCAuthPhotoController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setTitleName:@"Live Photo"];
    // Do any additional setup after loading the view.
}

- (void)configureAlloc {
    [super configureAlloc];
    self.step = @"LIVENESS_INFO";

}
- (void)configureUI {
    [super configureUI];
    [self _setCurrntStep:0];
    [self configureTable];
    [self.topRadiusView setTitleName:@"Live Photo:"];
    [self.tableView registerClass:[DCAuthTipsCell class] forCellReuseIdentifier:NSStringFromClass(DCAuthTipsCell.class)];
    [self.tableView registerClass:[DCAuthFooterView class] forHeaderFooterViewReuseIdentifier:NSStringFromClass(DCAuthFooterView.class)];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
}

- (void)takePhotoAction:(UIButton *)sender {
    DLog(@"%@",self);
    DCTakePhotoController *vc = [DCTakePhotoController new];
    vc.delegate = self;
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)nextBtnAtion:(UIButton *)sender {
    DLog(@"%@",sender);
}
#pragma mark - tableView
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    DCAuthTipsCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass(DCAuthTipsCell.class)];
    [cell setTipString:@"1. Please take a front-face photo\n2. Please shoot in a distraction-free environment\n3. Please do not cover your face\n4. Please hold your phone vertically and hold it firmly"];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return UITableViewAutomaticDimension;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 250;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 125;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 250)];
    headerView.backgroundColor = [UIColor colorWithString:@"#FFFFFF"];
    
    UIImageView *userPhotoImage = [[UIImageView alloc] init];
    _userPhotoImage = userPhotoImage;
    userPhotoImage.layer.cornerRadius = 16;
    userPhotoImage.layer.masksToBounds = YES;
    userPhotoImage.layer.borderColor = [UIColor colorWithString:@"#E1FE97"].CGColor;
    userPhotoImage.layer.borderWidth = 1;
    userPhotoImage.image = [UIImage imageNamed:@"userPhotoPlacehoder"];
    [headerView addSubview:userPhotoImage];
    [userPhotoImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(244);
        make.height.mas_equalTo(190);
        make.centerX.mas_equalTo(headerView.mas_centerX);
        make.top.mas_equalTo(15);
    }];

    UIButton *takePhoto = [UIButton buttonWithType:UIButtonTypeCustom];
    [takePhoto setImage:[UIImage imageNamed:@"takePhoto"] forState:UIControlStateNormal];
    [takePhoto addTarget:self action:@selector(takePhotoAction:) forControlEvents:UIControlEventTouchUpInside];
    [headerView addSubview:takePhoto];
    [takePhoto mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(userPhotoImage);
        make.centerY.mas_equalTo(userPhotoImage);
    }];
    
    UIImageView *statusImage = [UIImageView new];
    _statusImage = statusImage;
    statusImage.image = [UIImage imageNamed:@"photoStatus"];
    [headerView addSubview:statusImage];
    [statusImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(headerView.mas_centerX);
        make.top.mas_equalTo(userPhotoImage.mas_bottom).mas_offset(10);
        make.width.height.mas_equalTo(28);
    }];
    
    return headerView;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    DCAuthFooterView *footer = [tableView dequeueReusableHeaderFooterViewWithIdentifier:NSStringFromClass(DCAuthFooterView.class)];
    return footer;
}

- (void)takePhotoComplete:(NSData *)imageData {
    [self uploadImage:imageData];
}

- (void)uploadImage:(NSData *)imageData {
    [DCHttpRequest DCAuthUploadLiveImage:imageData CompleteBlock:^(NSURLSessionDataTask * _Nonnull operation, DCResponseData * _Nonnull responseObject) {
        if (responseObject.code == 200) {
            
        } else {
            [DCDCToast toastText:responseObject.message];
        }
    }];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end


@implementation DCAuthPhotoTipsCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style
                reuseIdentifier:reuseIdentifier];
    if (self) {
        [self configureUI];
    }
    return self;
}
- (void)configureUI {
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    self.backgroundColor = [UIColor colorWithString:@"#FFFFFF"];
    UILabel *tipLabel = [UILabel new];
    tipLabel.text = @"Tips";
    tipLabel.textColor = [UIColor colorWithString:@"#687666"];
    tipLabel.font = DCFont_PF_Regular(12);
    [self.contentView addSubview:tipLabel];
    [tipLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(0);
        make.left.mas_equalTo(45);
    }];
    
    UILabel *tipsLabel = [UILabel new];
    tipsLabel.text = @"1. Please take a front-face photo\n2. Please shoot in a distraction-free environment\n3. Please do not cover your face\n4. Please hold your phone vertically and hold it firmly";
    tipsLabel.textColor = [UIColor colorWithString:@"#C7C7C7"];
    tipsLabel.numberOfLines = 0;
    tipsLabel.font = DCFont_PF_Regular(12);
    [self.contentView addSubview:tipsLabel];
    [tipsLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(45);
        make.right.mas_equalTo(-45);
        make.top.mas_equalTo(tipLabel.mas_bottom).mas_offset(5);
        make.bottom.mas_equalTo(0);
    }];
    
}


@end
